#include "Buffer.h"

Buffer::Buffer()
{
	mData = NULL;
	mSize = 0;
}

Buffer::~Buffer()
{
	Clear();
}

void Buffer::Clear()
{
	SAFE_ARRAY_DELETE(mData);
	mSize = 0;
}

bool Buffer::Resize(u32 size)
{
	Clear();

	if (size < 0)
	{
		Error::Instance().SetMessage("Unable to resize buffer, size is too small!");
		return false;
	}

	mData = new c8[size + 1];
	if (mData == NULL)
	{
		Error::Instance().SetMessage("Unable to resize buffer, memory allocation failed!");
		return false;
	}

	mSize = size;
	mData[size] = '\0';

	return true;
}

c8* Buffer::GetData() const
{
	return mData;
}

u32 Buffer::GetSize() const
{
	return mSize;
}

Buffer& Buffer::operator =(const Buffer &buffer)
{
	Resize(buffer.mSize);
	std::memcpy(mData, buffer.GetData(), mSize);

	return (*this);
}

Buffer& Buffer::operator =(const String &string)
{
	Resize((u32)(string.size()));
	std::memcpy(mData,  string.c_str(), mSize);

	return (*this);
}

c8& Buffer::operator [](u32 index) const
{
	return mData[index];
}